
<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   
      <title>16.4.&nbsp;重识列表映射</title>
      <link rel="stylesheet" href="../diveintopython.css" type="text/css">
      <link rev="made" href="mailto:f8dy@diveintopython.org">
      <meta name="generator" content="DocBook XSL Stylesheets V1.52.2">
      <meta name="keywords" content="Python, Dive Into Python, tutorial, object-oriented, programming, documentation, book, free">
      <meta name="description" content="Python from novice to pro">
      <link rel="home" href="../toc/index.html" title="Dive Into Python">
      <link rel="up" href="index.html" title="第&nbsp;16&nbsp;章&nbsp;函数编程">
      <link rel="previous" href="filtering_lists.html" title="16.3.&nbsp;重识列表过滤">
      <link rel="next" href="data_centric.html" title="16.5.&nbsp;数据中心思想编程">
   </head>
   <body>
      <table id="Header" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td id="breadcrumb" colspan="5" align="left" valign="top">导航：<a href="../index.html">起始页</a>&nbsp;&gt;&nbsp;<a href="../toc/index.html">Dive Into Python</a>&nbsp;&gt;&nbsp;<a href="index.html">函数编程</a>&nbsp;&gt;&nbsp;<span class="thispage">重识列表映射</span></td>
            <td id="navigation" align="right" valign="top">&nbsp;&nbsp;&nbsp;<a href="filtering_lists.html" title="上一页: “重识列表过滤”">&lt;&lt;</a>&nbsp;&nbsp;&nbsp;<a href="data_centric.html" title="下一页: “数据中心思想编程”">&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3" id="logocontainer">
               <h1 id="logo"><a href="../index.html" accesskey="1">深入 Python :Dive Into Python 中文版</a></h1>
               <p id="tagline">Python 从新手到专家 [Dip_5.4b_CPyUG_Release]</p>
            </td>
            <td colspan="3" align="right">
               <form id="search" method="GET" action="http://www.google.com/custom">
                  <p><label for="q" accesskey="4">Find:&nbsp;</label><input type="text" id="q" name="q" size="20" maxlength="255" value=""> <input type="submit" value="搜索"><input type="hidden" name="domains" value="woodpecker.org.cn/diveintopython"><input type="hidden" name="sitesearch" value="www.woodpecker.org.cn/diveintopython"></p>
               </form>
            </td>
         </tr>
      </table>
      <!--#include virtual="/inc/ads" -->
      <div class="section" lang="zh_cn">
         <div class="titlepage">
            <div>
               <div>
                  <h2 class="title"><a name="regression.map"></a>16.4.&nbsp;重识列表映射
                  </h2>
               </div>
            </div>
            <div></div>
         </div>
         <div class="abstract">
            <p>你对使用<a href="../native_data_types/mapping_lists.html" title="3.6.&nbsp;映射 list">列表解析</a>映射列表的做法已经熟知。另一种方法可以完成同样的工作：使用内建 <tt class="function">map</tt> 函数。它的工作机理和 <a href="filtering_lists.html" title="16.3.&nbsp;重识列表过滤"><tt class="function">filter</tt></a> 函数类似。
            </p>
         </div>
         <div class="example"><a name="d0e36163"></a><h3 class="title">例&nbsp;16.10.&nbsp;<tt class="function">map</tt> 介绍
            </h3><pre class="screen">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput"><span class='pykeyword'>def</span><span class='pyclass'> double</span>(n):</span>
<tt class="prompt">...     </tt>return n*2
<tt class="prompt">...     </tt>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li = [1, 2, 3, 5, 9, 10, 256, -3]</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">map(double, li)</span>                       <a name="regression.map.1.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<span class="computeroutput">[2, 4, 6, 10, 18, 20, 512, -6]</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">[double(n) <span class='pykeyword'>for</span> n <span class='pykeyword'>in</span> li]</span>               <a name="regression.map.1.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<span class="computeroutput">[2, 4, 6, 10, 18, 20, 512, -6]</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">newlist = []</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput"><span class='pykeyword'>for</span> n <span class='pykeyword'>in</span> li:</span>                          <a name="regression.map.1.3"></a><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12">
<tt class="prompt">...     </tt><span class="userinput">newlist.append(double(n))</span>
<tt class="prompt">...     </tt>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">newlist</span>
<span class="computeroutput">[2, 4, 6, 10, 18, 20, 512, -6]</span></pre><div class="calloutlist">
               <table border="0" summary="Callout list">
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#regression.map.1.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left"><tt class="function">map</tt> 接受一个函数和一个列表作为参数，<sup>[<a name="d0e36239" href="#ftn.d0e36239">13</a>]</sup> 并对列表中的每个元素依次调用函数返回一个新的列表。在这个例子中，函数仅仅是将每个元素乘以 2。
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#regression.map.1.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">使用列表解析的方法你可以做到相同的事情。列表解析是在 <span class="application">Python</span> 2.0 版时被引入的；而 <tt class="function">map</tt> 则古老得多。
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#regression.map.1.3"><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">你如果坚持以 <span class="application">Visual Basic</span> 程序员自居，通过 <tt class="literal">for</tt> 循环的方法完成相同的任务也完全可以。
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         <div class="example"><a name="d0e36267"></a><h3 class="title">例&nbsp;16.11.&nbsp;<tt class="function">map</tt> 与混合数据类型的列表
            </h3><pre class="screen">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">li = [5, <span class='pystring'>'a'</span>, (2, <span class='pystring'>'b'</span>)]</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">map(double, li)</span>                       <a name="regression.map.2.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<span class="computeroutput">[10, 'aa', (2, 'b', 2, 'b')]</span></pre><div class="calloutlist">
               <table border="0" summary="Callout list">
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#regression.map.2.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">作为一个旁注，我想指出只要提供的那个函数能够正确处理各种数据类型，<tt class="function">map</tt> 对于混合数据类型列表的处理同样出色。在这里，<tt class="function">double</tt> 函数仅仅是将给定参数乘以 2，<span class="application">Python</span> 则会根据参数的数据类型决定正确操作的方法。对整数而言，这意味着乘 2；对字符串而言，意味着把自身和自身连接；对于元组，意味着构建一个包括原始元组全部元素和原始元组组合在一起的新元组。
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         <p>好了，玩够了。让我们来看一些真实代码。</p>
         <div class="example"><a name="d0e36303"></a><h3 class="title">例&nbsp;16.12.&nbsp;<tt class="filename">regression.py</tt> 中的 <tt class="function">map</tt></h3><pre class="programlisting">
    filenameToModuleName = <span class='pykeyword'>lambda</span> f: os.path.splitext(f)[0] <a name="regression.map.3.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
    moduleNames = map(filenameToModuleName, files)          <a name="regression.map.3.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></pre><div class="calloutlist">
               <table border="0" summary="Callout list">
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#regression.map.3.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">正如你在 <a href="../power_of_introspection/lambda_functions.html" title="4.7.&nbsp;使用 lambda 函数">第&nbsp;4.7&nbsp;节 “使用 lambda 函数”</a> 中所见，<tt class="literal">lambda</tt> 定义一个内联函数。也正如你在 <a href="../file_handling/os_module.html#splittingpathnames.example" title="例&nbsp;6.17.&nbsp;分割路径名">例&nbsp;6.17 “分割路径名”</a> 中所见，<tt class="function">os.path.splitext</tt> 接受一个文件名并返回一个元组 <tt class="literal">(<i class="replaceable">name</i>, <i class="replaceable">extension</i>)</tt>。因此 <tt class="function">filenameToModuleName</tt> 是一个接受文件名，剥离出其扩展名，然后只返回文件名称的函数。
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#regression.map.3.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">调用 <tt class="function">map</tt> 将把 <tt class="varname">files</tt> 列出的所有文件名传递给 <tt class="function">filenameToModuleName</tt> 函数，并且返回每个函数调用结果所组成的列表。换句话说，你剔除掉文件名的扩展名，并将剔除后的文件名存于 <tt class="varname">moduleNames</tt> 之中。
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         <p>如你在本章剩余部分将看到的，你可以将这种数据中心思想扩展到定义和执行一个容纳来自很多单个测试套件的测试的一个测试套件的最终目标。</p>
         <div class="footnotes">
            <h3 class="footnotetitle">Footnotes</h3>
            <div class="footnote">
               <p><sup>[<a name="ftn.d0e36239" href="#d0e36239">13</a>] </sup>同前，我需要指出 <tt class="function">map</tt> 可以接受一个列表、元组，或者一个像序列一样的对象。参见前面的关于 <tt class="function">filter</tt> 的脚注。
               </p>
            </div>
         </div>
      </div>
      <table class="Footer" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td width="35%" align="left"><br><a class="NavigationArrow" href="filtering_lists.html">&lt;&lt;&nbsp;重识列表过滤</a></td>
            <td width="30%" align="center"><br>&nbsp;<span class="divider">|</span>&nbsp;<a href="index.html#regression.divein" title="16.1.&nbsp;概览">1</a> <span class="divider">|</span> <a href="finding_the_path.html" title="16.2.&nbsp;找到路径">2</a> <span class="divider">|</span> <a href="filtering_lists.html" title="16.3.&nbsp;重识列表过滤">3</a> <span class="divider">|</span> <span class="thispage">4</span> <span class="divider">|</span> <a href="data_centric.html" title="16.5.&nbsp;数据中心思想编程">5</a> <span class="divider">|</span> <a href="dynamic_import.html" title="16.6.&nbsp;动态导入模块">6</a> <span class="divider">|</span> <a href="all_together.html" title="16.7.&nbsp;全部放在一起">7</a> <span class="divider">|</span> <a href="summary.html" title="16.8.&nbsp;小结">8</a>&nbsp;<span class="divider">|</span>&nbsp;
            </td>
            <td width="35%" align="right"><br><a class="NavigationArrow" href="data_centric.html">数据中心思想编程&nbsp;&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3"><br></td>
         </tr>
      </table>
      <div class="Footer">
         <p class="copyright">Copyright © 2000, 2001, 2002, 2003, 2004 <a href="mailto:mark@diveintopython.org">Mark Pilgrim</a></p>
         <p class="copyright">Copyright © 2001, 2002, 2003, 2004, 2005, 2006, 2007 <a href="mailto:python-cn@googlegroups.com">CPyUG (邮件列表)</a></p>
      </div>
   </body>
</html>